﻿/*****************************************************************
* Project: HFW Framework
*****************************************************************
* Developer: Hdhoan
* Date created: 11/2010
* Modified:
******************************************************************/
using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HFW.AssetManagement.Catalogue;
using HFW.Share;

namespace HFW.HFW
{
    public partial class frmMain : Form
    {

        #region Constructor & Support Methods to Load Form

        public frmMain()
        {
            InitializeComponent();
            cmdStandard.CommandClick+=new Janus.Windows.UI.CommandBars.CommandEventHandler(cmdStandard_CommandClick);
            this.Text = Applications.APPLICATION_NAME;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (DialogResult.No == MessageBoxEX.Show("Bạn có thật sự muốn thoát khỏi chương trình?", Applications.APPLICATION_TITLE_MESSAGEBOX, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
            {
                e.Cancel = true;
            }
            base.OnClosing(e);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            stsPnlUser.Text = Applications.CURRENT_USER_NAME;
            Start frmStart = new Start();
            ShowChildForm(frmStart);

        }


        private void frmMain_MdiChildActivate(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                this.mnuClose.Enabled = true;
                this.mnuCloseAll.Enabled = true;
            }
            else
            {
                this.mnuClose.Enabled = false;
                this.mnuCloseAll.Enabled = false;
            }

        }

        #endregion

        #region Timer support methods

        Timer timer = new Timer();
        private void timerCount_Tick(object sender, EventArgs e)
        {
            labelTime.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
            if (Applications.CURRENT_MESSAGE != string.Empty)
            {
                timer.Interval = 3000;
                timer.Tick += new EventHandler(timer_Tick);
                lblMessage.Text = Applications.CURRENT_MESSAGE;
                lblMessage.Visible = true;
                timer.Start();
            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            lblMessage.Text = Applications.CURRENT_MESSAGE = string.Empty;
            lblMessage.Visible = false;
            timer.Stop();
        }

        #endregion

        #region Toolbar cmd Manager

        private void cmdStandard_CommandClick(object sender, Janus.Windows.UI.CommandBars.CommandEventArgs e)
        {
            try
            {
                if (this.ActiveMdiChild != null)
                {
                    BaseForm cForm = (BaseForm)this.ActiveMdiChild;
                    switch (e.Command.Key)
                    {
                        case "btnNew":
                            cForm.fncAdd();
                            break;
                        case "btnSave":
                            cForm.fncSave();
                            break;
                        case "btnEdit":
                            cForm.fncEdit();
                            break;
                        case "btnDel":
                            cForm.fncDelete();
                            break;
                        case "btnPrint":
                            cForm.fncPrint();
                            break;
                        case "btnSearch":
                            cForm.fncFind();
                            break;
                        case "btnRefresh":
                            cForm.fncRefresh();
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBoxEX.Show(ex.ToString());
            }
        }

        #endregion

        #region Ribbon Command

        [DllImport("user32.dll")]
        public static extern bool LockWorkStation();

        private void frmMainRibbon_CommandClick(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
        {
            Cursor cur = Cursor.Current;
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                switch (e.Command.Name)
                {
                    case "mnuExit":
                        if (DialogResult.Yes == MessageBoxEX.Show("Bạn có thật sự muốn thoát khỏi chương trình?", Applications.APPLICATION_TITLE_MESSAGEBOX, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
                        {
                            Application.Exit();
                        }
                        break;
                    case "mnuLogOff":
                        Application.Restart();
                        break;
                    case "mnuClose":
                        if (this.ActiveMdiChild != null)
                        {
                            this.ActiveMdiChild.Close();
                        }
                        break;
                    case "mnuCloseAll":
                        foreach (Form frmChild in this.MdiChildren)
                        {
                            frmChild.Close();
                        }
                        break;
                    case "mnuLock":
                        LockWorkStation();
                        break;
                    case "mnuConfig":
                        {
                            break;
                        }
                    case "mnuAbout":
                        var dlabout = new ADM.frmAbout();
                        dlabout.ShowDialog();
                        break;
                    case "mnuHelp":
                        break;
                    case "mnuChangePass":
                        var cp = new ADM.frmChangePassword();
                        cp.ShowDialog();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBoxEX.Show(ex.ToString());
            }
            finally
            {
                Cursor.Current = cur;
            }
        }

        #endregion

        #region ShowForm

        protected bool ActiveChildForm(string strFromTitle)
        {
            bool bitReOpen = false;
            if (this.MdiChildren.Length > 0)
            {
                foreach (BaseForm cForm in this.MdiChildren)
                {
                    if (cForm.Text == strFromTitle)
                    {
                        cForm.Activate();
                        bitReOpen = true;
                        break;
                    }
                }
            }
            
            return bitReOpen;
        }

        protected void ShowChildForm(BaseForm actionForm)
        {
            if (!ActiveChildForm(actionForm.Text))
            {
                actionForm.MdiParent = this;
                actionForm.Show();
            }
        }
        private void VisibleNavigationPane(ref TreeView tree, DataTable dtRights)
        {
            if (Applications.CURRENT_USER_ID.ToUpper().Equals("ADMIN"))
            {
                return;
            }

            foreach (TreeNode itemG in tree.Nodes)
            {
                DataRow[] _drows = dtRights.Select("FunctionKey = '" + itemG.Name + "'");
                if (_drows.Length == 0)
                {
                    itemG.ForeColor = Color.Silver;
                }
            }
        }

        #endregion

        #region Dynamic Reporting Tree
        private void treeReport_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                TreeNode node = treeReport.SelectedNode;
                if (node.ForeColor == Color.Silver)
                    return;
                if (node != null)
                {
                    switch (node.Name)
                    {
                        
                        default:
                            if (node.Level == 2)
                                MessageBoxEX.Show("Chức năng này đang thiết kế.", Applications.APPLICATION_TITLE_MESSAGEBOX, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBoxEX.Show(ex.ToString());
            }
        }
        #endregion

        private void treeListing_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                TreeNode node = treeListing.SelectedNode;
                if (node.ForeColor == Color.Silver)
                    return;
                if (node != null)
                {
                    switch (node.Name)
                    {
                        case "DM_DVT":
                            var DM_DVT = new Asset_DonViTinh();
                            ShowChildForm(DM_DVT);
                            break;
                        case "DM_KH":
                            var DM_KH = new Asset_KhachHang();
                            ShowChildForm(DM_KH);
                            break;
                        case "DM_DVCC":
                            var DM_DVCC = new Asset_DonViCungCap();
                            ShowChildForm(DM_DVCC);
                            break;
                        case "DM_HANGSX":
                            var DM_HANGSX = new Asset_HangSanXuat();
                            ShowChildForm(DM_HANGSX);
                            break;
                        case "DM_TAISAN":
                            var DM_TAISAN = new Asset_TaiSan();
                            ShowChildForm(DM_TAISAN);
                            break;
                        default:
                            if (node.Level == 2)
                                MessageBoxEX.Show("Chức năng này đang thiết kế.", Applications.APPLICATION_TITLE_MESSAGEBOX, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBoxEX.Show(ex.ToString());
            }
        }

        private void treeADM_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                TreeNode node = treeADM.SelectedNode;
                if (node.ForeColor == Color.Silver)
                    return;
                if (node != null)
                {
                    switch (node.Name)
                    {
                        case "HT_USER":
                            var HT_USER = new ADM.frmUserManagement();
                            ShowChildForm(HT_USER);
                            break;
                        case "HT_USERGROUP":
                            var HT_USERGROUP = new ADM.frmGroup();
                            ShowChildForm(HT_USERGROUP);
                            break;
                        case "HT_PERMISSIONS":
                            var HT_PERMISSIONS = new ADM.frmPermissions();
                            ShowChildForm(HT_PERMISSIONS);
                            break;
                        case "HT_ABOUT":
                            var HT_ABOUT = new ADM.frmAbout();
                            HT_ABOUT.ShowDialog();
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBoxEX.Show(ex.ToString());
            }
        }

        private void treeINV_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                TreeNode node = treeINV.SelectedNode;
                if (node.ForeColor == Color.Silver)
                    return;
                if (node != null)
                {
                    switch (node.Name)
                    {
                        
                        default:
                            if (node.Level == 2)
                                MessageBoxEX.Show("Chức năng này đang thiết kế.", Applications.APPLICATION_TITLE_MESSAGEBOX, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBoxEX.Show(ex.ToString(), Applications.APPLICATION_TITLE_MESSAGEBOX, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


    }
}